package com.tjetc.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager;

import java.io.IOException;

public class Db {
    private static final SqlSessionManager sqlSessionManager;//默认自动提交(只适合单表)

    static {
        try {
            sqlSessionManager = SqlSessionManager.newInstance(Resources.getResourceAsStream(
                    "mybatis.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T getMapper(Class<T> clz) {
        return sqlSessionManager.getMapper(clz);
    }

    public static void start() {
        sqlSessionManager.startManagedSession();//开启事务
    }

    public static void commit() {
        sqlSessionManager.commit();//提交事务
    }

    public static void rollback() {
        sqlSessionManager.rollback();//回滚事务
    }

    public static void close() {
        sqlSessionManager.close();//关闭连接
    }
}
